﻿#region

using SqlSugar;

#endregion

namespace Vin.Extension.SqlSugar.Extension;

public static class PrimaryKeyExtension
{
    public static void AddAutoPrimaryKey(this VinSqlSugarClient client)
    {
        client.VinAop.DataExecuting += DataExecuting;
    }

    private static void DataExecuting(ISqlSugarClient client, object? oldValue, DataFilterModel entityInfo)
    {
        // 如果是插入并且是主键，并且没值，则自动生成
        if (entityInfo.OperationType == DataFilterType.InsertByObject && entityInfo.EntityColumnInfo.IsPrimarykey)
        {
            if (oldValue == null)
            {
                entityInfo.SetValue(SnowFlakeSingle.Instance.NextId());
            }
        }
    }
}